package thread.forkjoin;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;

@Slf4j
public class ForkJoinDemo {

    @Test
    public void test1() {

        ForkJoinPool forkJoinPool = ForkJoinPool.commonPool();

        // 1 外部任务
        forkJoinPool.execute(new Runnable() {
            @Override
            public void run() {
                log.debug("execute ...");
            }
        });

        ForkJoinTask<String> forkJoinTask = new RecursiveTask() {
            @Override
            protected String compute() {
                log.debug("compute ...");
                return "computed";
            }
        };
        // 2 内部任务
        forkJoinTask.fork();
        // 获取结果
        String join = forkJoinTask.join();
        log.debug("join res - {}", join);

    }

}
